package com.ibiaoke.goods.service;

import com.ibiaoke.common.base.Page;
import com.ibiaoke.common.consts.CommonConsts;
import com.ibiaoke.common.exception.ServiceException;
import com.ibiaoke.common.utils.AssertUtils;
import com.ibiaoke.common.utils.StringUtils;
import com.ibiaoke.goods.dao.GoodsDao;
import com.ibiaoke.goods.dao.ShopPriceDao;
import com.ibiaoke.goods.entity.Goods;
import com.ibiaoke.goods.entity.PtGoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/6/7.
 */
@Service("goodsService")
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private ShopPriceDao shopPriceDao;

    @Override
    public List<Goods> listGoods(Integer goodsId, Integer status, String goodsName, Integer cateId, Integer shopId, Page page) {
        return goodsDao.listGoods(goodsId, status, goodsName, cateId, shopId, page);
    }

    @Override
    public int deleteGoodsById(Integer goodsId) {
        return goodsDao.deleteGoodsById(goodsId);
    }

    @Override
    public int batchDeleteGoods(String goodsIds) {
        return goodsDao.batchDeleteGoods(goodsIds);
    }

    @Override
    public int batchUpdateGoodsStatus(String goodsIds, Integer status) {
        return goodsDao.batchUpdateGoodsStatus(goodsIds, status);
    }

    @Override
    @Transactional
    public int batchSaveGoods(Goods goods, String shopIds) {
        int count = 0;
        if (StringUtils.isNotEmpty(shopIds)) {
            String[] strings = shopIds.split(CommonConsts.SPLIT_CHAR);
            for (String shopId : strings) {
                AssertUtils.requireInt(shopId, "店铺id格式错误");
                goods.setShopId(Integer.valueOf(shopId));
                count += saveGoods(goods);
            }
            if (count < shopIds.length()) {
                throw new ServiceException(String.format("保存成功%d件，保存失败%d件", count, shopIds.length() - count));
            }
        } else {
            count = saveGoods(goods);
        }
        return count;
    }

    @Override
    public int saveGoods(Goods goods) {

        AssertUtils.notBlank(goods.getName(), "商品名称不能为空");

        AssertUtils.notNull(goods.getName(), "商品价格不能为空");

        AssertUtils.notNull(goods.getOldPrice(), "商品原价不能为空");

        AssertUtils.notNull(goods.getCateId(), "商品分类不能为空");

        AssertUtils.notNull(goods.getStockNum(), "商品库存不能为空");

        AssertUtils.notNull(goods.getStatus(), "商品上下架不能为空");

        AssertUtils.notBlank(goods.getSpec(), "商品规格不能为空");

        AssertUtils.notBlank(goods.getThumImage(), "商品缩略图不能为空");

        AssertUtils.notBlank(goods.getImage(), "商品主图不能为空");

        AssertUtils.notNull(goods.getShopId(), "请选择店铺");

        goods.setSaleNum(0);
        goods.setDelStatus(0);
        goods.setCreateTime(new Date());
        goods.setUpdateTime(new Date());

        int result = goodsDao.saveGoods(goods);
        return result;
    }

    @Override
    public int updateGoodsById(Goods goods) {
        if (goods.getGoodsId() == null) {
            throw new ServiceException("商品Id不能为空");
        }
        return goodsDao.updateByPrimaryKeySelective(goods);
    }

    @Override
    public Goods getGoodsByGoodsId(Integer goodsId, Integer shopId) {
        if (goodsId == null) {
            throw new ServiceException("商品编号不能为空");
        }
        return goodsDao.getGoods(goodsId, shopId);
    }

    @Override
    public List<Goods> listGoodsByGoodsClass(Integer cateId, Integer shopId, Page page) {
        return goodsDao.listGoods(cateId, shopId, page);
    }

    @Override
    public List<Goods> listGoods(List<Integer> goodsIds, Integer shopId) {
        return goodsDao.listByIds(goodsIds, shopId);
    }

    /*获取将要添加至拼团的商品*/
    @Override
    public Goods getGoodByPt(String goodName) {
        Goods good = goodsDao.getGoodByPt(goodName);
        return good;
    }
}
